import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

public class DelEmpForm extends JFrame{
	private static final long serialVersionUID = 1L;
	private JLabel dAsk;
	private JButton submit, cancel;
	private JPanel delPanel;
	ImageIcon ii = new ImageIcon(this.getClass().getResource("images/canprofbg.png"));
	private Image bgImage = ii.getImage();
	String confirmCan = "<html><h4>You are about to a cancel an employee profile.<br>Are you sure you want to cancel?</h4></html>";
	private int UID;

	public DelEmpForm() {
		setTitle("Delete Profile");

		delPanel = new JPanel() {
			private static final long serialVersionUID = 1L;

			protected void paintComponent(Graphics g){
				g.drawImage(bgImage, 0, 0, null);
				super.paintComponent(g);
			}
		};

		//construct components
		dAsk = new JLabel (confirmCan);
		submit = new JButton ("Yes");
		cancel = new JButton ("No");

		//adjust size and set layout
		setPreferredSize (new Dimension (380, 200));
		setResizable(false);
		setLayout (null);
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

		//add components
		delPanel.add (dAsk);
		delPanel.add (submit);
		delPanel.add (cancel);
		add(delPanel);

		delPanel.setSize(380, 200);
		delPanel.setOpaque(false);
		delPanel.setLayout(null);
		delPanel.setVisible(true);

		//set component bounds (only needed by Absolute Positioning)
		dAsk.setBounds (30, 70, 400, 60);
		submit.setBounds (145, 135, 100, 25);
		cancel.setBounds (255, 135, 100, 25);

		submit.setBounds (135, 130, 65, 25);
		cancel.setBounds (210, 130, 65, 25);

		dAsk.setForeground(Color.WHITE);
		submit.setForeground(Color.WHITE);
		cancel.setForeground(Color.WHITE);
		submit.setBackground(Color.BLACK);
		cancel.setBackground(Color.BLACK);


		cancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				AdminForm.goToPage(1);
			}
		});
		submit.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				setVisible(false);
				try{
					ConnectionFactory myFactory = ConnectionFactory.getFactory();
					Connection conn = myFactory.getConnection();

					PreparedStatement can;
					can = conn.prepareStatement("UPDATE employee set status = 'Inactive' where idemployee = ?");
					can.setInt(1, UID);
					can.executeUpdate();
					JOptionPane.showMessageDialog(delPanel, "You have successfully deleted the profile.");
					AdminForm.goToPage(0);
				}catch(Exception ex){
					ex.printStackTrace();
				}

			}
		});

	}

	public void setUID(int id){
		UID = id;
	}
}
